Ask students about python experience, general coding experience. Make sure everyone can open python by typing 'python' into a terminal


In [2]:
import sys
sys.version


Out[2]:
'2.7.9 |Anaconda 2.1.0 (x86_64)| (default, Dec 15 2014, 10:37:34) \n[GCC 4.2.1 (Apple Inc. build 5577)]'

Conda is a python distribution that comes with many of the packages that you'd find useful in astronomy. It also has some other benefits (namely the Intel Math Kernel Library) and appears to work well on all platforms (where Canopy has some issues on Windows machines). For the purposes of this course, I don't think you'll need more than the default Python installation, but if you plan to use Python in your life, then I'd recommend just grabbing this now. Free with a .edu email!


In [3]:
2+3


Out[3]:
5

In [4]:
3**4


Out[4]:
81

Woah! What's going on here? This is sure strange. Let's talk about what types of data all these are!


In [9]:
a = 3*4

In [10]:
a


Out[10]:
12

In [5]:
5/4


Out[5]:
1

In [6]:
type(2)


Out[6]:
int

In [9]:
type(a)


Out[9]:
int

In [10]:
b = 4

In [11]:
type(a/b)


Out[11]:
int

In [12]:
type(2.3)


Out[12]:
float

Whenever you have a variable in Python, it has some type. This tells us something about how it is stored, and what you can do with it! Common data types we'll see this summer: "bool" (True/False), "int", "long" (long integers), "float", "str", "list", "tuple", "dict"


In [13]:
True


Out[13]:
True

In [14]:
type(True)


Out[14]:
bool

So what is a float? It's a way to represent numbers we consider from binary-coded values


In [20]:
0.5**(12)


Out[20]:
0.000244140625

But there are some limitations! There is a maximum precision for each number. This means a small number + a big number may be just that big number.


In [19]:
1.0 + 0.0000000000000000000001


Out[19]:
1.0

And a big number - a big number can really cause problems! This above is only an error of 1 part in 10^14, but the one below gives 100% error!


In [23]:
1.0000000000000000000000000000001 - 1.0


Out[23]:
0.0

There are things to be careful about then


In [35]:
0.1 + 0.2 == 0.3


Out[35]:
False

In [36]:
print("{0:.20f}".format(0.1 + 0.2))
print("{0:.20f}".format(0.3))


0.30000000000000004441
0.29999999999999998890

It means there's a minimum number


In [21]:
1e-400


Out[21]:
0.0

and a maximum number


In [22]:
1e400


Out[22]:
inf

For more, read the Wiki on floating point numbers. For much more, Heath's "Scientific Computing." Lecture slides available at http://heath.cs.illinois.edu/scicomp/

For now, the important things are that: Python will keep the same data type as the result of an operation. If two things have different data types, it will default to the more general case. Most important for division!

4/3


In [26]:
4.0/3.0


Out[26]:
1.3333333333333333

In [25]:
4.0/3


Out[25]:
1.3333333333333333

In [28]:
4.0/3 # this is a float divided by an integer


Out[28]:
1.3333333333333333
Breakout number one time!

In small groups, and without googling, figure out what the following operators do. Don't be afraid to ask questions, either of other groups or me! +, -, *, /, //, **, %, >, <, !=, ==. Extra credit: &, |. Some of these will do exactly what you think they will, some might not!

Bring it back in

Everything we've done so far has been in the terminal. There are a few other ways to run python as well! One is an iPython notebook, which I'm using here. To open one of these, type "ipython notebook" at the command line. the other way is to create a python file, and run that from the command line. Let's do that now. Use your favorite text editor to create and open a file called "hello.py". I'm using an application called Brackets, you probably have one of "nano" or "emacs" preinstalled on your computer.

In there, write the following:


In [30]:
print("Hello World")


Hello World

Now let's run this file! From the command line:

python hello.py

What do you see?

Hello world is a string.

We can put any of the lines of code we've written earlier into a python file, run it, and find the same results as we found earlier. But right now let's write something new. Let's use an "if statement."


In [3]:
value = 0.0
if value == 0:
    pass
elif value > 0.0:
    print 'yo'
else:
    print 'woooooo!'

what's going to happen? Why? What if we change "value" to 1.0? -1.0? 'True'? Let's test all of these!


In [4]:
wordlist = ['this', 'is', 'a', 'list', 'of', 'words']

In [5]:
for i, j in enumerate(wordlist):
    print i, j


0 this
1 is
2 a
3 list
4 of
5 words

In [6]:
print wordlist[0]


this

In [7]:
print wordlist[-1]


words

In [8]:
for i in xrange(10):
    print i


0
1
2
3
4
5
6
7
8
9

Why no #10?

Breakout #2 time!

Using your new knowledge of for loops and if statements, write a program that prints the numbers from 1 to 100. But for multiples of three print “Fizz” instead of the number and for the multiples of five print “Buzz”. For numbers which are multiples of both three and five print “FizzBuzz”.

Save this code somewhere you'll rememeber, because we're going to start up by modifying this code tomorrow...


In [ ]: